/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tecsup.oper.servlet.plan;

import com.tecsup.oper.util.FuncionesERP;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Leonel
 */
@WebServlet(name = "AgregadoServlet", urlPatterns = {"/AgregadoServlet"})
public class AgregadoServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        int produccion = 0;
        int inventario[];
        int numInvIni = 0;
        int numInvFin = 0;
        int numEmpIni = 0;
        int nmeses = 0;
        
        String InvIni = request.getParameter("txtInvIni");
        String InvFin= request.getParameter("txtInvFin");
        String EmpIni = request.getParameter("txtEmpIni");
        String meses = request.getParameter("meses");
        String valores[] = request.getParameterValues("cantidades");
        String capacidad = request.getParameter("txtcapacidad");
        String costoInv = request.getParameter("txtCostoInv");
        String costoBO = request.getParameter("txtCostoBO");
        String costoContr = request.getParameter("txtCostoContr");
        String costoDesp = request.getParameter("txtCostoDesp");
        String recargo = request.getParameter("txtRecargo");
        String salario = request.getParameter("txtSalario");
        String stock = request.getParameter("txtStock");
        
        int nvalores[] = new int [valores.length];
        
        numInvIni = Integer.parseInt(InvIni);
        numInvFin = Integer.parseInt(InvFin);
        nmeses = Integer.parseInt(meses);
        int ncapacidad = Integer.parseInt(capacidad);
        numEmpIni = Integer.parseInt(EmpIni);
        double ncostoInv = Double.valueOf(costoInv);
        double ncostoBO = Double.valueOf(costoBO);
        double ncostoContr = Double.valueOf(costoContr);
        double ncostoDesp = Double.valueOf(costoDesp);
        double nrecargo = Double.valueOf(recargo);
        double nsalario = Double.valueOf(salario);
        int nstock = Integer.parseInt(stock);
        
        for (int i=0; i<valores.length; i++){
            nvalores[i]=Integer.parseInt(valores[i]);            
        }
        
        FuncionesERP erp = new FuncionesERP();
        
        produccion = erp.produccion(nvalores, numInvIni, numInvFin, nmeses);
        
        inventario = erp.inventario(nvalores, produccion, numInvIni, nmeses);
        
        int empleados[] = erp.empleados(produccion, ncapacidad,nmeses,numEmpIni);
        
        int contrataciones[] = erp.contrataciones(empleados, nmeses);
        
        int despidos[] = erp.despidos(empleados, nmeses);
        
        double suplementos[] = erp.suplementos(produccion, nmeses, ncapacidad);
        
        String tablaCostos = erp.costos(nmeses, inventario, ncostoInv, ncostoBO, 
                contrataciones,ncostoContr,despidos,ncostoDesp, empleados, nsalario, 
                suplementos, nrecargo, nstock);
        
        //Convirtiendo el array inventario, contrataciones,suplementos, despidos y empleados en String []
        String s_inventario[] = new String [inventario.length];
              
        for (int k = 0; k<inventario.length; k++){
            s_inventario[k]=String.valueOf(inventario[k]);
        }
        //--
        String s_empleados[] = new String [empleados.length];
              
        for (int k = 0; k<empleados.length; k++){
            s_empleados[k]=String.valueOf(empleados[k]);
        }
        //--
        String s_contrataciones[] = new String [contrataciones.length];
              
        for (int k = 0; k<contrataciones.length; k++){
            s_contrataciones[k]=String.valueOf(contrataciones[k]);
        }
        //--
        String s_despidos[] = new String [despidos.length];
              
        for (int k = 0; k<despidos.length; k++){
            s_despidos[k]=String.valueOf(despidos[k]);
        }
        //--
        String s_suplementos[] = new String [suplementos.length];
              
        for (int k = 0; k<suplementos.length; k++){
            s_suplementos[k]=String.valueOf(suplementos[k]);
        }
        //--
        request.setAttribute("txtInvIni",InvIni);
        request.setAttribute("txtInvFin",InvFin);
        request.setAttribute("cantidades", valores);
        request.setAttribute("numero", nmeses);       
        request.setAttribute("produccion", produccion);
        request.setAttribute("inventario", s_inventario);
        request.setAttribute("empleados", s_empleados);
        request.setAttribute("txtcapacidad", capacidad);
        request.setAttribute("contrataciones", s_contrataciones);
        request.setAttribute("despidos", s_despidos);
        request.setAttribute("suplementos", s_suplementos);
        request.setAttribute("tablaCostos", tablaCostos);
        request.setAttribute("txtCostoInv",costoInv);
        request.setAttribute("txtCostoBO",costoBO);
        request.setAttribute("txtCostoContr",costoContr);
        request.setAttribute("txtCostoDesp",costoDesp);
        request.setAttribute("txtSalario",salario);
        request.setAttribute("txtRecargo",recargo);
        request.setAttribute("txtStock",stock);
        
        RequestDispatcher rd = request.getRequestDispatcher("produccion.jsp");
        rd.forward(request, response);
        
        //PrintWriter pw = response.getWriter();
        //pw.println("Hola k tal como están?");
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
